{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bb754e1d-8c05-46e3-b156-f6fd0184a039",
   "metadata": {},
   "source": [
    "# In-Context Learning and Prompting"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c530c2bc-17b5-4a4c-94c7-d6a5cd785be1",
   "metadata": {},
   "source": [
    "## In-context learning: Provide examples of the input text within the context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ef144502-f9a1-4a81-9a18-ae5a47a8ca48",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pip install transformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ec8ef079-b4ca-487a-85cd-1975f4f49838",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "generator = pipeline(\"text-generation\", model=\"EleutherAI/pythia-1.4b\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed5c2ab9-2798-4187-afab-68de05e0d415",
   "metadata": {},
   "source": [
    "> Note: Pythia is a small GPT-like LLM. For more information, please refer to the original paper, [Pythia: A Suite for Analyzing Large Language Models Across Training and Scaling](https://arxiv.org/abs/2304.01373)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b6bef96-6819-4af6-b65d-7ce3068af47d",
   "metadata": {},
   "source": [
    "**Without in-context examples**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d4a3a8f9-52b3-4b58-be0b-ae01d8d3801c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Translate this sentence:\n",
      "German: 'Wo ist die naechste Bushaltestelle?'\n",
      "English: 'Where is the next bus stop?'\n",
      "\n",
      "A:\n",
      "\n",
      "The German word for \"next\" is \"nach\", which is the past tense of \"sein\".\n",
      "The English word for \"next\" is \"nach\", which is the past tense of \"be\".\n",
      "The German word for \"next\" is \"nach\", which is the past tense\n"
     ]
    }
   ],
   "source": [
    "prompt = \"\"\"Translate this sentence:\n",
    "German: 'Wo ist die naechste Bushaltestelle?'\"\"\"\n",
    "\n",
    "generated_text = generator(prompt, max_length=100, temperature=1, top_k=0, top_p=0)\n",
    "print(generated_text[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7e8f984-2361-4fb3-b5c1-0302357a01e6",
   "metadata": {},
   "source": [
    "**With in-context examples**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b877ab43-199f-4997-9a6f-1d5597bece1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Translate the following German sentences into English:\n",
      "\n",
      "Example 1:\n",
      "German: \"Ich liebe Pfannkuchen.\"\n",
      "English: \"I love pancakes.\"\n",
      "\n",
      "Example 2:\n",
      "German: \"Das Wetter ist heute schoen.\"\n",
      "English: \"The weather is nice today.\"\n",
      "\n",
      "Translate this sentence:\n",
      "German: \"Wo ist die naechste Bushaltestelle?\"\n",
      "English: \"Where is the next bus stop?\"\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "prompt = \"\"\"Translate the following German sentences into English:\n",
    "\n",
    "Example 1:\n",
    "German: \"Ich liebe Pfannkuchen.\"\n",
    "English: \"I love pancakes.\"\n",
    "\n",
    "Example 2:\n",
    "German: \"Das Wetter ist heute schoen.\"\n",
    "English: \"The weather is nice today.\"\n",
    "\n",
    "Translate this sentence:\n",
    "German: \"Wo ist die naechste Bushaltestelle?\"\n",
    "\"\"\"\n",
    "\n",
    "generated_text = generator(prompt, max_length=102, temperature=1, top_k=0, top_p=0)\n",
    "print(generated_text[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91d7a6f2-410a-4966-9c1a-d4f90a1a5e83",
   "metadata": {},
   "source": [
    "### Prompt tuning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "363ae482-fea0-4ae1-9875-c3c3a85b4cb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Translate the following German sentences into English:\n",
      "\n",
      "Example 1:\n",
      "Translate the German sentence 'Ich liebe Pfannkuchen.' into English: 'I love pancakes.' \n",
      "\n",
      "Example 2:\n",
      "Translate the German sentence 'Das Wetter ist heute schoen.' into English: 'The weather is nice today.'\n",
      "\n",
      "Translate the German sentence 'Wo ist die naechste Bushaltestelle?' into English: \n",
      "\n",
      "Where is the next bus stop?\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# \"Translate the German sentence '{german_sentence}' into English: \"\n",
    "\n",
    "prompt = \"\"\"Translate the following German sentences into English:\n",
    "\n",
    "Example 1:\n",
    "Translate the German sentence 'Ich liebe Pfannkuchen.' into English: 'I love pancakes.' \n",
    "\n",
    "Example 2:\n",
    "Translate the German sentence 'Das Wetter ist heute schoen.' into English: 'The weather is nice today.'\n",
    "\n",
    "Translate the German sentence 'Wo ist die naechste Bushaltestelle?' into English: \n",
    "\"\"\"\n",
    "\n",
    "generated_text = generator(prompt, max_length=105, temperature=1, top_k=0, top_p=0)\n",
    "print(generated_text[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "95d20e4b-dc99-496b-b110-765138cc990c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Translate the following German sentences into English:\n",
      "\n",
      "Example 1:\n",
      "German: 'Ich liebe Pfannkuchen.' | English: 'I love pancakes.' \n",
      "\n",
      "Example 2:\n",
      "German: 'Das Wetter ist heute schoen.' | English: 'The weather is nice today.'\n",
      "\n",
      "German: 'Wo ist die naechste Bushaltestelle?' | English: \n",
      "\n",
      "'Where is the next bus stop?'\n"
     ]
    }
   ],
   "source": [
    "# \"German: '{german_sentence}' | English: \"\n",
    "\n",
    "prompt = \"\"\"Translate the following German sentences into English:\n",
    "\n",
    "Example 1:\n",
    "German: 'Ich liebe Pfannkuchen.' | English: 'I love pancakes.' \n",
    "\n",
    "Example 2:\n",
    "German: 'Das Wetter ist heute schoen.' | English: 'The weather is nice today.'\n",
    "\n",
    "German: 'Wo ist die naechste Bushaltestelle?' | English: \n",
    "\"\"\"\n",
    "\n",
    "generated_text = generator(prompt, max_length=96, temperature=1, top_k=0, top_p=0)\n",
    "print(generated_text[0][\"generated_text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a7b3bf49-d875-49a8-9689-ed4b37367810",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Example 1:\n",
      "From German to English: 'Ich liebe Pfannkuchen.' -> 'I love pancakes.' \n",
      "\n",
      "Example 2:\n",
      "From German to English: 'Das Wetter ist heute schoen.' -> 'The weather is nice today.'\n",
      "\n",
      "Example 3:\n",
      "From German to English: 'Wo ist die naechste Bushaltestelle?' -> : \n",
      "\n",
      "Example 4:\n",
      "From\n"
     ]
    }
   ],
   "source": [
    "# \"From German to English: '{german_sentence}' ->  \"\n",
    "\n",
    "prompt = \"\"\"Example 1:\n",
    "From German to English: 'Ich liebe Pfannkuchen.' -> 'I love pancakes.' \n",
    "\n",
    "Example 2:\n",
    "From German to English: 'Das Wetter ist heute schoen.' -> 'The weather is nice today.'\n",
    "\n",
    "Example 3:\n",
    "From German to English: 'Wo ist die naechste Bushaltestelle?' -> : \n",
    "\"\"\"\n",
    "generated_text = generator(prompt, max_length=90, temperature=1, top_k=0, top_p=0)\n",
    "print(generated_text[0][\"generated_text\"])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
